/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 3/17/2012 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define PhieuThuDao class. This class inherit from  PhieuThuDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class PhieuThuDao : PhieuThuDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public PhieuThuDao() { }
        static PhieuThuDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(PhieuThuDao).FullName);
        }
        private static PhieuThu GetPhieuThuDataReaderByMaPhieuBanHang(IDataReader reader)
        {
            String methodName = "GetPhieuThuDataReaderByMaPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuThu result = new PhieuThu();
                //read MaPhieuThu field data.

                result.MaPhieuThu = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuThu");

                //read MaPhieuBanHang field data.

               // result.MaPhieuBanHang = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuBanHang");

                //read NguoiThu field data.

                result.NguoiThu = ManagerDao.GetstringDataFromDataReader(reader, "NguoiThu");

                //read NgayThu field data.

                result.NgayThu = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayThu");

                //read ThanhTien field data.

                result.ThanhTien = ManagerDao.GetdoubleDataFromDataReader(reader, "ThanhTien");

                //read CurencyCode field data.

                result.CurencyCode = ManagerDao.GetstringDataFromDataReader(reader, "CurencyCode");

                //read Notes field data.

                result.Notes = ManagerDao.GetstringDataFromDataReader(reader, "Notes");


                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static List<PhieuThu> GetByMaPhieuBanHang(Int64 maPhieuBanHang)
        {
            String methodName = "GetByMaPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                
                List<PhieuThu> result = new List<PhieuThu>();
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetPhieuThuByPhieuBanHang");
                //Add value "MaPhieuThu"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhieuBanHang", DbType.Int64, maPhieuBanHang);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                while(reader.Read())
                {                
                    result.Add(GetPhieuThuDataReaderByMaPhieuBanHang(reader));
                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static bool DeleteByMaPhieuBanHang(Int64 MaPhieuBanHang)
        {
            String methodName = "DeleteByMaPhieuBanHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeletePhieuThuByPhieuBanHang");
                //Add value "MaPhieuThu"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuBanHang", DbType.Int64, MaPhieuBanHang);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }

        public static bool InsertUpdate(PhieuThu obj)
        {
            bool result = false;
            String methodName = "InsertUpdate";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertUpdatePhieuThu");


            //Add value "MaPhieuThu"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuThu", DbType.Int64, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuThu));
            //Add value "MaPhieuBanHang"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuBanHang", DbType.Int64, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuBanHang));
            //Add value "NguoiThu"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiThu", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.NguoiThu));
            //Add value "NgayThu"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayThu", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.NgayThu));
            //Add value "ThanhTien"  paramater.
            ManagerDao.AddInParameter(cmd, "ThanhTien", DbType.Double, ConvertValueObject.CastObjValueToInsertDB(obj.ThanhTien));
            //Add value "CurencyCode"  paramater.
            ManagerDao.AddInParameter(cmd, "CurencyCode", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.CurencyCode));
            //Add value "Notes"  paramater.
            ManagerDao.AddInParameter(cmd, "Notes", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.Notes));
            //Add value "CreatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
            //Add value "CreatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
            //Add value "LastUpdatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
            //Add value "LastUpdatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
    }
}
        #endregion